import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class NoticeWidget extends StatefulWidget {
  List<String> list;
  int duration = 2000;
  double itemHeight = 40.0;

  NoticeWidget(this.list);

  @override
  _NoticeWidgetState createState() => _NoticeWidgetState();
}

class _NoticeWidgetState extends State<NoticeWidget> {
  var scrollController = ScrollController();
  var index = 0;

  @override
  void initState() {
    // TODO: implement initState
    Timer.periodic(Duration(milliseconds: widget.duration), (Timer timer) {
      if (scrollController != null && index == widget.list.length - 1) {
        index = 0;
        scrollController.jumpTo(0.0);
      } else if (scrollController != null && scrollController.hasClients) {
//        ScrollController not attached to any scroll views????(scrollController.hasClients)
        scrollController.animateTo((index + 1) * widget.itemHeight,
            duration: new Duration(seconds: 2), curve: Curves.ease);
        index++;
      }
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(left: 10, right: 10),
      height: widget.itemHeight,
      child: ListView.builder(
        itemBuilder: (BuildContext context, int index) {
          return Container(
            alignment: Alignment.centerLeft,
            height: widget.itemHeight,
            child: Row(
              children: <Widget>[
                Icon(Icons.notifications),
                Text(widget.list[index]),
              ],
            ),
          );
        },
        itemCount: widget.list.length,
        controller: scrollController,
      ),
    );
  }
}
